from django.shortcuts import render,HttpResponse,redirect,reverse
from .models import hostGroup,hostInfo,hostDetail,hostMangerUser
from users.views import auth_user
from .utils.getData import basicData
import json
# Create your views here.
@auth_user
def hostGroupIndex(request):
    allHostGroups = hostGroup.objects.all()
    return render(request, 'cmdb-groups.html', {'allHostGroup': allHostGroups})
@auth_user
def addHostGroup(request):
    ret = {}
    groupname = request.POST.get('groupname',None)
    groupdesc = request.POST.get('groupdesc',None)
    groupObj = hostGroup.objects.get_or_create(hostGroupName=groupname,hostGroupDes=groupdesc)
    obj,status = groupObj
    if status:
        getGroupObj = hostGroup.objects.get(hostGroupName=groupname)
        ret['status']=status
        ret['groupid']=getGroupObj.id
        return HttpResponse(json.dumps(ret))
    else:
        return HttpResponse('False')
@auth_user
def delHostGroup(request):
    groupid=request.POST.get('groupid',None)
    if groupid != None:
        groupid = int(groupid)
        groupobj = hostGroup.objects.get(id=groupid)
        if groupobj.hostsNum==0:
            groupobj.delete()
            return HttpResponse('ok')
        else:
            return HttpResponse('False')
@auth_user
def editHostGroup(request):
    pass

@auth_user
def hostIndex(request):
    allHostGroups = hostGroup.objects.all()
    hostLists = hostInfo.objects.all()
    manageruser = hostMangerUser.objects.all()
    return render(request, 'cmdb-hosts.html',{'allHostGroups':allHostGroups,'hostlists':hostLists,'managers':manageruser})
@auth_user
def addHost(request):
    # 添加完成后，查询所有hostUpdate=0的主机，进行更新，获取CPU，硬盘，内存信息
    hostid = request.POST.get('hostid', None)
    hostname = request.POST.get('hostname',None)
    hostpublicip = request.POST.get('hostpublicip',None)
    hostsshport = request.POST.get('hostport',None)
    hostservices = request.POST.get('hostservices',None)
    hostsgroupid = request.POST.get('hostgroupid',None)
    hostlocation = request.POST.get('hostlocation',None)
    hostuserid = request.POST.get('hostuser',None)
    flag = request.POST.get('flag',None)
    if flag == 'add':
        addhostobj=hostInfo()
        addhostobj.hostName=hostname
        addhostobj.hostPublicIP=hostpublicip
        if len(hostsshport) != 0:
            addhostobj.hostSSHPort=int(hostsshport)
        addhostobj.hostSeviceList=hostservices
        addhostobj.hostgroup_id=int(hostsgroupid)
        addhostobj.manageruser_id=int(hostuserid)
        addhostobj.hostLocation=int(hostlocation)
        addhostobj.save()

        hostgroupobj = hostGroup.objects.get(id=int(hostsgroupid))
        hostgroupobj.hostsNum +=1
        print(hostgroupobj.hostsNum)
        hostgroupobj.save()
        # 更新主机
        hostlist = hostInfo.objects.filter(hostUpdate=0)
        for h in range(len(hostlist)):
            hid = hostlist[h].id
            getHostDetail(hid)
            thishostobj = hostInfo.objects.filter(id = hid).first()
            thishostobj.hostUpdate=1
            thishostobj.save()
    elif flag =='edit':
        editHostObj = hostInfo.objects.get(id=hostid)
        editHostObj.hostName = hostname
        editHostObj.hostPublicIP = hostpublicip
        editHostObj.hostSSHPort = int(hostsshport)
        editHostObj.hostSeviceList = hostservices
        editHostObj.hostgroup_id = int(hostsgroupid)
        editHostObj.manageruser_id = int(hostuserid)
        editHostObj.hostLocation = int(hostlocation)
        editHostObj.save()
    return HttpResponse('ok')
@auth_user
def delHost(request):
    hostid = request.POST.get('hostid', None)
    hostobj = hostInfo.objects.get(id=hostid)
    groupid = hostobj.hostgroup_id
    hostobj.delete()
    hostgroupobj = hostGroup.objects.get(id=int(groupid))
    hostgroupobj.hostsNum -=1
    hostgroupobj.save()
    return HttpResponse('ok')
'''
@auth_user
def editHost(request):
    hostid = request.POST.get('hostid',None)
    hostname = request.POST.get('hostname',None)
    hostpublicip = request.POST.get('hostpublicip',None)
    hostport = request.POST.get('hostport',22)
    hostservices = request.POST.get('hostservices',None)
    hostsgroupid = request.POST.get('hostgroupid',None)
    hostlocation = request.POST.get('hostlocation',None)
    hostuserid = request.POST.get('hostuser', None)
    editHostObj = hostInfo.objects.get(id=hostid)
    editHostObj.hostName=hostname
    editHostObj.hostPublicIP=hostpublicip
    editHostObj.hostSSHPort=int(hostport)
    editHostObj.hostSeviceList=hostservices
    editHostObj.hostgroup_id=int(hostsgroupid)
    editHostObj.manageruser_id = int(hostuserid)
    editHostObj.hostLocation=int(hostlocation)
    editHostObj.save()
    return HttpResponse('ok')
'''
def getHostDetail(hostid):
    hostObj = hostInfo.objects.filter(id=hostid).first()
    hostip = hostObj.hostPublicIP
    hostport = hostObj.hostSSHPort
    manageruserid = hostObj.manageruser_id
    hostmanagerobj = hostMangerUser.objects.filter(id=manageruserid).first()
    username = hostmanagerobj.managerName
    passwd = hostmanagerobj.managerPasswd
    data = basicData(hostip,username,passwd,hostport)
    cpu = data.get('cpu',None)
    mem = data.get('mem',None)
    disk = data.get('disk',None)
    privateip = data.get('privateip',None)
    detailobj = hostDetail()
    detailobj.hostinfo_id = hostid
    detailobj.hostCPU = cpu
    detailobj.hostMem = mem
    detailobj.hostDisk = disk
    detailobj.hostPrivateIP = privateip
    detailobj.save()

@auth_user
def addManagerUser(request):
    managerusername = request.POST.get('managername',None)
    managerpwd = request.POST.get('managerpwd',None)
    hostMangerUser.objects.get_or_create(managerName=managerusername,managerPasswd=managerpwd)
    userobj = hostMangerUser.objects.get(managerName=managerusername)
    ret={}
    ret['userid']=userobj.id
    return HttpResponse(json.dumps(ret))

@auth_user
def delManagerUser(request):
    hostuserid = request.POST.get('userid', None)
    hostuserid=int(hostuserid)
    hostnum = hostInfo.objects.filter(manageruser_id=hostuserid).count()
    if hostnum ==0:
        hostMangerUser.objects.get(id=hostuserid).delete()
        return HttpResponse('ok')
    else:
        return HttpResponse('False')
